
function swakSmackPotCalc() {
	swakLastRowIndex = -1;
//	alert('swakSmackPotCalc entry');

	//check to see if open/showing..if so, close
	var ssp = swaksmack.SmackShopDoc.getElementById('swakSmackPot');
	if (ssp != null) {
		try {
			if (ssp.style.width!='0px') {
				ssp.style.width='0px';
				ssp.style.height='0px';
				return;
			}
		}
		catch (x){}
	}

	var retail = swaksmack.SmackShopDoc.getElementById('retailPrice');
	if (retail == null) return;
	var spt = swaksmack.SmackShopDoc.getElementById('smackPotTable');
	if (spt == null) return;
	
//	alert('pre get smackpot');
	var spotDiv = swaksmack.SmackShopDoc.getElementById('smackPot');
//	alert('smackpop div ' + spotDiv);
	var cf = getElementsByClassName('clearFloat',spotDiv);
	
//	alert(spt.offsetParent.offsetLeft + ' table offsetLeft  ' + spt.offsetLeft );
//	alert(spt.offsetParent.offsetTop + ' table offsetParent offsetTop  ' + spt.offsetTop);
//	var ttop = -spt.offsetTop + 225;  //spt.offsetParent.offsetTop 
//	var lleft =  spt.offsetLeft +185; //spt.offsetParent.offsetLeft +
	
	var ttop = 0;
	var lleft = 0;
	
	//var ssp = swaksmack.SmackShopDoc.getElementById('swakSmackPot');
	
	var price = retail.textContent;
	//alert('price ' + price);
	if (ssp == null) {
		var str = swakBuildSmackPotHelp(price, ttop, lleft);
		//alert('swakSmackPotCalc done building str');
		//var spheaderDiv = swaksmack.SmackShopDoc.getElementById('smackPot'); //smackPotHeader
		
		
		 var spresultDiv = swaksmack.SmackShopDoc.getElementById('smackPotResults'); //smackPotHeader
		
			var spdiv = swaksmack.SmackShopDoc.createElement('div');
			spdiv.setAttribute('id','swakSmackPotContainer');
			spdiv.innerHTML = str;
			//alert(spotDiv + ' spotDiv, pre insert cf length ' + cf.length);
			//spheaderDiv.appendChild(spdiv);
			if (cf.length == 1) {
				spotDiv.insertBefore(spdiv,cf[0]);
			}
			else if (cf.length == 16) {
				spotDiv.insertBefore(spdiv,cf[15]); //was cf[0]
				//	spotDiv.insertBefore(spdiv,spresultDiv); //was cf[0]
			}
			else {
				alert('Something has changed...this makes Mindstorm cry.');
			}

}
else {
	//alert('show, dont build');
	var spdiv = swaksmack.SmackShopDoc.getElementById('swakSmackPotContainer'); 
	var str =  swakBuildSmackPotHelp(price,ttop,lleft);
	spdiv.innerHTML = str; 
	ssp.style.width='200px';
	ssp.style.height='450px';
}

//var retail = swaksmack.SmackShopDoc.getElementById('retailPrice');
//alert('calling smackpot money ranges');
//swakBuildSmackPotPriceRanges(retail.textContent);

}

function swakBuildSmackPotHelp(retail, top, left) {
	
	var str='';
		str+='<div style="z-index: 9999; width: 200px; height: 450px;overflow: hidden; position: relative; visibility: visible; top: ' + top + 'px; left: ' + left + 'px; background: #660000;" id="swakSmackPot">';
		str+='            <script language="javascript">';
		str+='            function smakspClose()';
		str+='            {';
		str+='               document.getElementById(\'swakSmackPot\').style.width=\'0px\';';
		str+='               document.getElementById(\'swakSmackPot\').style.height=\'0px\'';		
		str+='            }';
		str+='            <\/script>';
		str+=swakBuildSmackPotPriceRanges(retail);
		str+='<\/div>';
		return str;
	
}


function swakBuildSmackPotPriceRanges(retailPrice) {
	//alert('swakBuildSmackPotPriceRanges entry ' + retailPrice);
	if (retailPrice == null) return null;
	retailPrice = retailPrice.replace(/\$/g, "");
	var price = parseFloat(retailPrice);
	if (isNaN(price) == true) return null;
	//alert('retail is a price ' + price);
	var d0 = price;
	var d5 = swakMoneyFormat(price*0.95); //0-5
	var d10 = swakMoneyFormat(price*0.9); //5-10
	var d15 = swakMoneyFormat(price*0.85); //10-15
	var d20 = swakMoneyFormat(price*0.8); //15-20
	var d25 = swakMoneyFormat(price*0.75);
	var d30 = swakMoneyFormat(price*0.7);
	var d35 = swakMoneyFormat(price*0.65);
	var d40 = swakMoneyFormat(price*0.6);
	var d45 = swakMoneyFormat(price*0.55);
	var d50 = swakMoneyFormat(price*0.5);
	var d55 = swakMoneyFormat(price*0.45);
	var d60 = swakMoneyFormat(price*0.4);
	var d65 = swakMoneyFormat(price*0.35);
	var d70 = swakMoneyFormat(price*0.3); //65-70
	var d100 = swakMoneyFormat(0.00); //70-100
	
	//alert(d0 + ', d35: ' +d35 + ', ' + d100);
	var r05 = swakBuildRange(d0,d5);
	var r10 = swakBuildRange(d5,d10);
	var r15 = swakBuildRange(d10,d15);
	var r20 = swakBuildRange(d15,d20);
	var r25 = swakBuildRange(d20,d25);
	var r30 = swakBuildRange(d25,d30);
	var r35 = swakBuildRange(d30,d35);
	var r40 = swakBuildRange(d35,d40);
	var r45 = swakBuildRange(d40,d45);
	var r50 = swakBuildRange(d45,d50);
	var r55 = swakBuildRange(d50,d55);
	var r60 = swakBuildRange(d55,d60);
	var r65 = swakBuildRange(d60,d65);
	var r70 = swakBuildRange(d65,d70);
	var r100 = swakBuildRange(d70,d100);
//	alert('range 05 '+ r05);
//	alert('range 100 '+ r100);

//
//time test
var startTime = swaksmack.SS_JF.getDealStartedAt();
var deltaPerSec = swaksmack.SS_JF.getSliderDelta();
var timeArray = new Array();


timeArray[0] = Math.round(5/deltaPerSec)*1000 + startTime;
timeArray[1] = Math.round(10/deltaPerSec)*1000 + startTime;
timeArray[2] = Math.round(15/deltaPerSec)*1000 + startTime;
timeArray[3] = Math.round(20/deltaPerSec)*1000 + startTime;
timeArray[4] = Math.round(25/deltaPerSec)*1000 + startTime;
timeArray[5] = Math.round(30/deltaPerSec)*1000 + startTime;
timeArray[6] = Math.round(35/deltaPerSec)*1000 + startTime;
timeArray[7] = Math.round(40/deltaPerSec)*1000 + startTime;
timeArray[8] = Math.round(45/deltaPerSec)*1000 + startTime;
timeArray[9] = Math.round(50/deltaPerSec)*1000 + startTime;
timeArray[10] = Math.round(55/deltaPerSec)*1000 + startTime;
timeArray[11] = Math.round(60/deltaPerSec)*1000 + startTime;
timeArray[12] = Math.round(65/deltaPerSec)*1000 + startTime;
timeArray[13] = Math.round(70/deltaPerSec)*1000 + startTime;
timeArray[14] = Math.round(100/deltaPerSec)*1000 + startTime;

for (var i=0; i<timeArray.length; i++) {
	var rdate = new Date(timeArray[i]);
	timeArray[i] = rdate.formatDate("h:i");
}

//
	
	var table = '<table cellspacing="0" cellpadding="0" border="0">';
	table+='<tr class=\'swakspheader\'><td>Range</td><td>Price</td><td>End Time</td></tr>';
	table+=buildTableRow('0-5',r05,timeArray[0],'swakodd');
	table+=buildTableRow('5-10',r10,timeArray[1],'swakeven');
	table+=buildTableRow('10-15',r15,timeArray[2],'swakodd');
	table+=buildTableRow('15-20',r20,timeArray[3],'swakeven');
	table+=buildTableRow('20-25',r25,timeArray[4],'swakodd');
	table+=buildTableRow('25-30',r30,timeArray[5],'swakeven');
	table+=buildTableRow('30-35',r35,timeArray[6],'swakodd');
	table+=buildTableRow('35-40',r40,timeArray[7],'swakeven');
	table+=buildTableRow('40-45',r45,timeArray[8],'swakodd');
	table+=buildTableRow('45-50',r50,timeArray[9],'swakeven');
	table+=buildTableRow('50-55',r55,timeArray[10],'swakodd');
	table+=buildTableRow('55-60',r60,timeArray[11],'swakeven');
	table+=buildTableRow('60-65',r65,timeArray[12],'swakodd');
	table+=buildTableRow('65-70',r70,timeArray[13],'swakeven');
	table+=buildTableRow('70-100',r100,timeArray[14],'swakodd');
	table+='            <tr>';
	table+='            <td colspan="3">';
	table+='            <input type="button" value="Close" onclick="smakspClose();" style="width: 119px"\/><\/td>';
	table+='            <\/tr>';
	table+='</table>';
	
//	alert('table ' + table);
	return table;
}

function buildTableRow(per,range,time,class) {
	var str = '';
	if (class != null) {
		str+='<tr class=\'' + class + '\'><td>'+per;
	}
	else {
		str+='<tr><td>'+per;
	}
	str+='</td><td>' +range;
	str+='</td><td>' + time;
	str+='</td></tr>';
	return str;
}

function swakBuildRange(num1, num2) {
	if (num1 == null || num2 == null) return '???';
	if (num2 == 0) num2 = '0.00';
	var range = '$' +num1 +' - $' +num2;
	return range;
}

function swakMoneyFormat(num) {
	var d1 = Math.floor(num*100+0.50000000001);
	var cents = d1%100;
	if(cents<10) {
		cents = '0' + cents;
	}
	var value = Math.round(num*100)/100;
	value = value.toString();
	try {
		var dec = value.indexOf('.');
		if (dec != -1) {
			value = value.substring(0,dec);
			value = value + '.' + cents;
		}
		else {
			//no decimal
			value = value + '.00';
		}
  }
  catch (x) {
  	value = Math.round(num*100)/100;
  }
	
	return value;
}

function swakFindSmackPotRow(discount) {
	if (discount == null) return -1;
	var row = -1;
	if (discount<=5.0) {
		row = 0;
	}
	if (discount >5.0 && discount<=10.0) {
		row = 1;
	}
	else if (discount >10.0 && discount<=15.0) {
		row = 2;
	}	
	else if (discount >15.0 && discount<=20.0) {
		row = 3;
	}
	else if (discount >20.0 && discount<=25.0) {
		row = 4;
	}	
	else if (discount >25.0 && discount<=30.0) {
		row = 5;
	}
	else if (discount >30.0 && discount<=35.0) {
		row = 6;
	}
	else if (discount >35.0 && discount<=40.0) {
		row = 7;
	}
	else if (discount >40.0 && discount<=45.0) {
		row = 8;
	}				
	else if (discount >45.0 && discount<=50.0) {
		row = 9;
	}	
	else if (discount >50.0 && discount<=55.0) {
		row = 10;
	}
	else if (discount >55.0 && discount<=60.0) {
		row = 11;
	}
	else if (discount >60.0 && discount<=65.0) {
		row = 12;
	}
	else if (discount >65.0 && discount<=70.0) {
		row = 13;
	}	
	else if (discount >70.0) {
		row = 14;
	}	
	row += swakSmackPotTableRowOffset;
	//swakLastRowIndex = row;
	return row;				
}


var swakLastRowIndex = -1;
var swakSmackPotTableRowOffset = 1;

function swakHighlightCurrentRange(rowIndex,force) {
	if (rowIndex == null || rowIndex == -1) return;
	if (force == null) force = false;
	if (force == false && rowIndex == swakLastRowIndex) {
		return;
	}
	try {
	//	alert('hl row rowIndex ' + rowIndex);
		var sptable = swaksmack.SmackShopDoc.getElementById('smackPotTable'); 
		if (sptable == null) return;
		var rows = sptable.getElementsByTagName("tr");
		if (rows == null || rows.length == 0) return;
		//var currentRow = rows[rowIndex];
		for (var j=0; j<rows.length;j++) {
			var cells = rows[j].getElementsByTagName("td");
			for (var i=0;i<cells.length;i++) {
				if (j == rowIndex) {
					var rangeColor = SS_getPreference(SMACK_DISPLAY_SMACKPOT_RANGECOLOR);
					cells[i].style.background = rangeColor;  //#5f2369  cce680  511c5c //default JF#392D4E  , current fav '#4d1a58
				}
				else {
					cells[i].style.background = '';
				}
			}
		}
		swakLastRowIndex = rowIndex;
	}
	catch(x) {
		alert(x);
	}
}